package com.mall.huohuo.openapi.controller.member.address;

import com.mall.huohuo.common.domain.HuohuoUser;
import com.mall.huohuo.common.security.WebUtils;
import com.mall.huohuo.exception.BusinessException;
import com.mall.huohuo.openapi.controller.member.MemberConstant;
import com.mall.huohuo.result.PageResult;
import com.mall.huohuo.result.Result;
import com.mall.huohuo.service.address.domain.MemberAddress;
import com.mall.huohuo.service.address.service.MemberAddressInfoService;
import com.mall.huohuo.service.address.service.MemberAddressOperateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author zyconding
 * @version AuthParam.java, v 0.1 2020-08-07 16:23 zyconding
 */

@RestController
@RequestMapping(MemberConstant.MEMBER_PREFIX+"/address")
public class AddressController  {

    @Autowired
    private MemberAddressInfoService memberAddressInfoService;

    @Autowired
    private MemberAddressOperateService memberAddressOperateService;

    @GetMapping("/index")
    public PageResult<MemberAddress> getMemberAddressList(Integer pageSize, Integer pageNum) {
        if (pageSize == null) {
            pageSize = 100;
        }
        if (pageNum == null) {
            pageNum = 1;
        }

        HuohuoUser currentUser = WebUtils.getCurrentUser();
        PageResult<MemberAddress> memberAddressList = memberAddressInfoService.
                getMemberAddressList(currentUser.getId(), pageNum, pageSize);
        return memberAddressList;
    }

    /**
     * 获取单个收货地址
     * @param addressId 收货地址id
     * @return 收货地址详情
     */
    @GetMapping("/{addressId}")
    public Result<MemberAddress> getMemberAddressDetail(@PathVariable Long addressId){
        return Result.success(memberAddressInfoService.getMemberAddressDetail( WebUtils.getCurrentUser().getId(),addressId));
    }


    /**
     * 新增收货地址
     * @param memberAddress 收货地址
     * @return 是否成功
     */
    @PostMapping("/save")
    public Result<Boolean> saveAddress(@RequestBody  MemberAddress memberAddress){
        Long memberId = WebUtils.getCurrentUser().getId();
        return Result.success(memberAddressOperateService.saveMemberAddress(memberId, memberAddress));
    }


    /**
     * 更新收货地址
     * @param addressId 收货地址id
     * @return 是否成功
     */
    @PostMapping("/default/{addressId}")
    public Result<Boolean> updateDefault(@PathVariable Long addressId){
        Long memberId = WebUtils.getCurrentUser().getId();

        MemberAddress memberAddress = new MemberAddress();
        memberAddress.setIsDefault(true);
        memberAddress.setId(addressId);

        return Result.success(memberAddressOperateService.updateMemberAddress(memberId, memberAddress));
    }




    /**
     * 异常地址 默认地址不能删除
     * @param addressId 收货地址id
     * @return 是否成功
     */
    @PostMapping("/delete/{addressId}")
    public Result<Boolean> deleteMemberAddress(@PathVariable Long addressId){
        Long memberId = WebUtils.getCurrentUser().getId();
        MemberAddress memberAddressDetail = memberAddressInfoService.getMemberAddressDetail(memberId, addressId);
        if (memberAddressDetail.getIsDefault()) {
            throw new BusinessException("默认地址是不能删除。",102);
        }

        MemberAddress memberAddress = new MemberAddress();
        memberAddress.setId(addressId);
        memberAddress.setIsDeleted(true);

        return Result.success(memberAddressOperateService.updateMemberAddress(memberId, memberAddress));
    }


    /**
     * 异常地址 默认地址不能删除
     * @param memberAddress 收货地址
     * @return 是否成功
     */
    @PostMapping("/delete/{addressId}")
    @Valid
    public Result<Boolean> updateMemberAddress(@RequestBody MemberAddress memberAddress){
        Long memberId = WebUtils.getCurrentUser().getId();
        return Result.success(memberAddressOperateService.updateMemberAddress(memberId, memberAddress));
    }



}
